﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using vHCS.SYS.Entities;
using Core.Message;
using Core.ExceptionHandling;
using Core.NTier.UserInterface;
using Core.Controls.Message;
using Core.Message.Entities;
using Telerik.Web.UI;
using System.Collections.ObjectModel;
using vHCS.SYS.Business;
using Core.Common.Session;
namespace vHCS.Web.MdlSYS.Controls
{
    /// <summary>
    /// Thông tin tìm kiếm nhân viên
    /// </summary>
    /// Author:HungDV
    public partial class HR_ctrlUserSearch : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack == false)
            {
               
            }
        }

        #region Control Property
        /// <summary>
        /// Giá trị các control
        /// </summary>
        public SYS_UserSeachDTO ItemData
        {
            get
            {
                SYS_UserSeachDTO controlData = new SYS_UserSeachDTO();
                try
                {
                    //Mã nhân viên
                    controlData.EmployeeCode = this.txtEmpCode.Text.Trim();
                    //Tên nhân viên
                    controlData.EmployeeName = this.txtEmpName.Text.Trim();
                    //Barcode
                    controlData.Barcode = this.txtBarcode.Text.Trim();
                    //Báo cáo cho
                    controlData.ReportToUserId = this.cboReportTo.SelectedValue.Trim();
                    //Tên đăng nhập
                    controlData.Account = this.txtUsernames.Text.Trim();

                    long lngTem = 0;
                    //Loại HD
                    long.TryParse(this.cboContractType.SelectedValue, out lngTem);
                    controlData.ContractTypeId = lngTem;
                    //Loại NV
                    long.TryParse(this.cboEmpType.SelectedValue, out lngTem);
                    controlData.EmployeeTypeId = lngTem;
                    //Cơ cấu tổ chức
                    long.TryParse(this.cboOrgChart.SelectedValue, out lngTem);
                    controlData.OrgChartId = lngTem;

                    //Ranking theo cơ cấu chuyên môn
                    long.TryParse(this.cboProRanking.SelectedValue, out lngTem);
                    controlData.ProRankingId = lngTem;
                    //Grade theo cơ cấu chuyên môn
                    long.TryParse(this.cboProGrade.SelectedValue, out lngTem);
                    controlData.ProGradeId = lngTem;
                    //Cây cơ cấu chuyên môn
                    HIE_JobChartNode proSelected = this.ParseNodeId(this.cboProChart.SelectedValue);
                    controlData.ProJobFamilyId = proSelected.JobFamilyId;
                    controlData.ProJobGroupId = proSelected.JobGroupId;
                    controlData.ProSubGroupId = proSelected.SubGroupId;

                    //Ranking theo cơ cấu quản lý
                    long.TryParse(this.cboMgtRanking.SelectedValue, out lngTem);
                    controlData.MgtRankingId = lngTem;
                    //Grade theo cơ cấu quản lý
                    long.TryParse(this.cboMgtGrade.SelectedValue, out lngTem);
                    controlData.MgtGradeId = lngTem;
                    //Cây cơ cấu chuyên môn
                    HIE_JobChartNode mgtSelected = this.ParseNodeId(this.cboMgtChart.SelectedValue);
                    controlData.MgtJobFamilyId = mgtSelected.JobFamilyId;
                    controlData.MgtJobGroupId = mgtSelected.JobGroupId;
                    controlData.MgtSubGroupId = mgtSelected.SubGroupId;

                    //Chức vụ
                    long.TryParse(this.cboJobTitle.SelectedValue, out lngTem);
                    controlData.JobTitleId = lngTem;
                    //Cấp bậc
                    long.TryParse(this.cboJobLevel.SelectedValue, out lngTem);
                    controlData.JobLevelId = lngTem;
                    //Văn phòng làm việc
                    long.TryParse(this.cboWorkingLocation.SelectedValue, out lngTem);
                    controlData.WorkingLocationId = lngTem;
                    //Tình trạng
                    SYS_TerminationStatus status = SYS_TerminationStatus.All;
                    Enum.TryParse<SYS_TerminationStatus>(this.rbtTerminationStatus.SelectedValue, out status);
                    controlData.TeminationStatus = status;
                }
                catch (Exception ex)
                {
                    AlertMessage message = UserInterfaceExceptionHandler.HandleExcetion(ex);
                    MessageBox.Show(message);
                }
                return controlData;
            }
        }
        #endregion

        #region Event
        /// <summary>
        /// Hàm xử lý sự kiện khi thay đổi giá trị của cấp bậc
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void cboJobLevel_SelectedIndexChanged(object sender, Telerik.Web.UI.RadComboBoxSelectedIndexChangedEventArgs e)
        {
            PopulateJobTitle();
        }
        /// <summary>
        /// Hàm xử lý sự kiện click trên cây cơ cấu quản lý
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void trvMgtChart_NodeClick(object sender, RadTreeNodeEventArgs e)
        {
            this.PopulateJobTitle();
        }

        /// <summary>
        /// Hàm xử lý sự kiện click trên cây cơ cấu chuyên môn
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void trvProChart_NodeClick(object sender, RadTreeNodeEventArgs e)
        {
            //Nếu cơ cấu quản lý đã chọn, không lọc theo cơ cấu chuyên môn
            if (
                string.IsNullOrEmpty(cboMgtChart.SelectedValue) == true
                || cboMgtChart.SelectedValue == HIE_HierarchyCollection.JobChartNullNodeValue
                )
            {
                this.PopulateJobTitle();
            }
        }
        #endregion

        #region Proctected Helper Function
       

        /// <summary>
        /// Phân rã Id của node. 
        /// Cấu trúc Id của Node: JobFamilyId;JobGroupId;SubGroupId
        /// </summary>
        /// <param name="nodeId"></param>
        /// <returns></returns>
        protected HIE_JobChartNode ParseNodeId(string nodeId) {
            try
            {
                if (string.IsNullOrEmpty(nodeId)) return new HIE_JobChartNode();
                else if (nodeId.Trim() == "") return new HIE_JobChartNode();
                else
                {                    
                    string[] arrId = nodeId.Split(';');
                    if (arrId == null || arrId.Length < 3) return new HIE_JobChartNode();
                    HIE_JobChartNode node = new HIE_JobChartNode();
                    node.JobFamilyId = long.Parse(arrId[0].Trim());
                    node.JobGroupId = long.Parse(arrId[1].Trim());
                    node.SubGroupId = long.Parse(arrId[2].Trim());
                    return node;
                }
            } 
            catch (Exception ex)
            {
                AlertMessage message = UserInterfaceExceptionHandler.HandleExcetion(ex);
                MessageBox.Show(message);
            }
            return new HIE_JobChartNode();
        
        }

        /// <summary>
        /// Load danh sách cho cboJobTitle
        /// </summary>
        protected void PopulateJobTitle() {
            try
            {
                long jobLevelId = 0;
                HIE_JobChartNode selectedNode = new HIE_JobChartNode();
                long.TryParse(cboJobLevel.SelectedValue, out jobLevelId);

                //Nếu cơ cấu quản lý không có giá trị thì chọn cơ cấu chuyên môn
                if (string.IsNullOrEmpty(cboMgtChart.SelectedValue)
                    || cboMgtChart.SelectedValue==HIE_HierarchyCollection.JobChartNullNodeValue)
                    selectedNode = this.ParseNodeId(cboProChart.SelectedValue);
                else
                    selectedNode = this.ParseNodeId(this.cboMgtChart.SelectedValue);

                dtsJobTitle.SelectParameters["jobLevelId"].DefaultValue = jobLevelId.ToString();
                dtsJobTitle.SelectParameters["jobFamilyId"].DefaultValue = selectedNode.JobFamilyId.ToString();
                dtsJobTitle.SelectParameters["jobGroupId"].DefaultValue = selectedNode.JobGroupId.ToString();
                dtsJobTitle.SelectParameters["subGroupId"].DefaultValue = selectedNode.SubGroupId.ToString();
                dtsJobTitle.CacheClear();
                cboJobTitle.DataBind();

            }
            catch (Exception ex)
            {
                AlertMessage message = UserInterfaceExceptionHandler.HandleExcetion(ex);
                MessageBox.Show(message);
            }
        }

        #endregion

        #region Public Function
        /// <summary>
        /// Xóa giá trị của tất cả control
        /// </summary>
        public void ResetForm() { 
            try{
                this.txtBarcode.Text = string.Empty;
                this.txtEmpCode.Text = string.Empty;
                this.txtEmpName.Text = string.Empty;
                this.txtUsernames.Text = string.Empty;
                this.cboContractType.SelectedValue = string.Empty;
                this.cboEmpType.SelectedValue = string.Empty;
                this.cboJobLevel.SelectedValue = string.Empty;

                

                this.cboOrgChart.Items[0].Value = string.Empty;
                this.cboOrgChart.Items[0].Text = string.Empty;
                this.cboOrgChart.SelectedIndex = 0;

               
                this.cboMgtChart.Items[0].Value = string.Empty;
                this.cboMgtChart.Items[0].Text = string.Empty;
                this.cboMgtChart.SelectedIndex = 0;
                this.cboMgtGrade.SelectedValue = string.Empty;
                this.cboMgtRanking.SelectedValue = string.Empty;

                this.cboProChart.Items[0].Value = string.Empty;
                this.cboProChart.Items[0].Text = string.Empty;
                this.cboProChart.SelectedIndex = 0;

                this.cboProGrade.SelectedValue = string.Empty;
                this.cboProRanking.SelectedValue = string.Empty;
                this.cboReportTo.SelectedValue = string.Empty;
                this.cboWorkingLocation.SelectedValue = string.Empty;

                this.dtsJobTitle.CacheClear();
                this.cboJobTitle.SelectedValue = string.Empty;
                this.cboJobTitle.DataBind();

                this.rbtTerminationStatus.SelectedIndex = 2;
            }
            catch (Exception ex)
            {
                AlertMessage message = UserInterfaceExceptionHandler.HandleExcetion(ex);
                MessageBox.Show(message);
            }
        }
        #endregion
    }
}